﻿using ASP;
using Ext.Net;
using NOAH.CMS.Repository;

using System;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;

public partial class admin_Modules_NewsEvent_Contact_AddOrEdit : System.Web.UI.Page
{

    private string id = string.Empty;

    public void AddOrEdit(object sender, EventArgs e)
    {
        try
        {
            using (ContactRepository contactRepositiory = new ContactRepository())
            {
                Contact feedback = null;
                if (this.id != "0")
                {
                    feedback = contactRepositiory.Get(int.Parse(this.id));
                }
                else
                {
                    feedback = new Contact();
                }
                feedback.Name = this.txtName.Text;
                feedback.Mobile = this.txtMobile.Text;
                feedback.Email = this.txtEmail.Text;
                feedback.Content = this.txtContent.Text;
                feedback.Answer = this.txtAnswer.Text;
                feedback.IsPublish = new bool?(this.chkPublish.Checked);
                //feedback.ModifiedDate = DateTime.Now;
                if (this.id == "0")
                {
                    feedback.CreatedOn = new DateTime?(DateTime.Now);
                    contactRepositiory.Add(feedback);
                }
                contactRepositiory.SubmitChanges();
                ExtNet.Msg.Hide();
                Util.ShowMessage(Constants.UPDATE_SUCCESS);
                BasePage.RegisterCallBackFromChild(this.ScriptManager2, string.Empty);
            }
        }
        catch
        {
            Util.ShowMessage2(Constants.UPDATE_FAILURE);
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        id = base.Request.QueryString["id"] ?? "0";

        this.txtName.Focus(false, 100);
        if (!ExtNet.IsAjaxRequest)
        {
            this.ShowDetail();
        }
    }

    private void ShowDetail()
    {
        if (this.id != "0")
        {
            using (ContactRepository contactRepositiory = new ContactRepository())
            {
                Contact feedback = contactRepositiory.Get(int.Parse(this.id));
                this.txtName.Text = feedback.Name;
                this.txtMobile.Text = feedback.Mobile;
                this.txtEmail.Text = feedback.Email;
                this.txtContent.Text = feedback.Content;
                this.txtAnswer.Text = feedback.Answer;
                bool? temp = feedback.IsPublish;
                this.chkPublish.Checked = temp.HasValue ? temp.GetValueOrDefault() : false;
            }
        }
    }
}